package com.jinzecloud.reportapproval.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.jinzecloud.reportapproval.bean.AuthenticationTask;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.InspResuPro;
import com.jinzecloud.reportapproval.service.InspResuProService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 检验结果报告审批流程
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-05 09:27:36
 */
@RestController
@RequestMapping("reportapproval/inspresupro")
@RequiredArgsConstructor
public class InspResuProController {
    @Autowired
    private InspResuProService inspResuProService;
    private final AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:inspresupro:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = inspResuProService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:inspresupro:info")
    public R info(@PathVariable("id") Integer id){
		InspResuPro inspResuPro = inspResuProService.getById(id);

        return R.ok().put("inspResuPro", inspResuPro);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:inspresupro:save")
    public R save(@RequestBody InspResuPro inspResuPro){
        inspResuPro.setStatus(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        inspResuPro.setUserId(adminService.getIdByName(authentication.getName()));
		R r = inspResuProService.saveInspResuPro(inspResuPro);

        return r;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:inspresupro:update")
    public R update(@RequestBody InspResuPro inspResuPro){
		inspResuProService.updateById(inspResuPro);

        return R.ok();
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(inspResuProService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody AuthenticationTask authenticationTask) {
        inspResuProService.updateById(new InspResuPro().setId(authenticationTask.getId()).setStatus(StateEnum.CHECK_DETECT.getState()));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return inspResuProService.audit(auditParam);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:inspresupro:delete")
    public R delete(@RequestBody Integer[] ids){
		inspResuProService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/export/{id}")
    public R export(@PathVariable("id") Integer id) {
        String path = inspResuProService.export(id);
        return R.ok().put("path", path);
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return inspResuProService.viewPdf(id);
    }
    @RequestMapping("/viewWord/{id}")
    public String viewWord(@PathVariable("id") Integer id){
        return inspResuProService.viewWord(id);
    }

}
